package com.company.common.server.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class CloudSmsAliyunKit {


    public static void sendSms(String phoneNumbers, String templateCode, Map<String, String> templateParam) {
        CloudSmsAliyunKit.send(phoneNumbers, templateCode, templateParam, null, null);
    }


    public static void send(String phoneNumbers, String templateCode, Map<String, String> templateParam, String extendId, String extendCode) {
        SmsAliyunConfig config = new SmsAliyunConfig();

        DefaultProfile profile = DefaultProfile.getProfile("default", config.getSmsAccessKeyId(), config.getSmsAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("SignName", config.getSmsSignName());
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("TemplateCode", templateCode);
        if (templateParam != null && templateParam.size() > 0) {
            request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParam));
        }
        if (StringUtils.isNotEmpty(extendId)) {
            request.putQueryParameter("OutId", extendId);
        }
        if (StringUtils.isNotEmpty(extendCode)) {
            request.putQueryParameter("SmsUpExtendCode", extendCode);
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("+++++++sms+++++++" + response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
